<script setup lang="ts">
import { ref } from 'vue';
import KnowledgeCard from './KnowledgeCard.vue'
import type{ KnowledgeList, KnowledgeType } from '@/types/consult';
import { getKnowledgePage } from '@/services/consult';

const props = defineProps<{
  type: KnowledgeType
}>()

// 加载中的状态
const loading = ref(false)
// 是否加载完成
const finished = ref(false)
const list = ref<KnowledgeList>([])

// 查询参数
const params = {
  type:props.type,
  current:1,
  pageSize:5
}
// 滚动到底部
const onLoad = async()=>{
  // 加载后台数据
  const res = await getKnowledgePage(params)
  list .value.push(...res.data.rows)
  // 判断加载完成
  if(params.current >= res.data.total){
    finished.value = true
  }else{
    params.current++
  }
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list v-model:loading="loading" :finished="finished"
    finished-text="没有更多了"
    @load="onLoad">
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
